package com.coolgor.coldot.schedule.Friday;

import com.coolgor.coldot.dto.stock.StockInfoExecution;
import com.coolgor.coldot.entity.stock.MarketDaily;
import com.coolgor.coldot.entity.stock.MarketMinite;
import com.coolgor.coldot.entity.stock.StockInfo;
import com.coolgor.coldot.enums.stock.StockInfoOperationStateEnum;
import com.coolgor.coldot.service.stock.MarketDailyService;
import com.coolgor.coldot.service.stock.MarketMiniteService;
import com.coolgor.coldot.service.stock.StockInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class SinajsService {

    @Autowired
    StockInfoService stockInfoService;

    @Autowired
    StockService stockService;

    @Autowired
    MarketMiniteService marketMiniteService;

    @Autowired
    MarketDailyService marketDailyService;

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");



    public boolean holdMarketMinite(String result){
        if (null==result || "".equals(result)) {
            System.out.println("holdMarketMinite -> result input errer -> " + result);
            return false;
        }
        List<String> stockEmptyNameCodeList = stockService.getEmptyNameCodeList();
        StockInfo stockInfo;
        StockInfo stockInfoCondition;
        String[] list = result.strip().split("var");
        MarketMinite marketMinite ;
        for (String s : list) {
            // 整理字符串，并分割出一个数组
            String infoString = s.strip()
                                .replace("hq_str_","")
                                .replace("\"","")
                                .replace(";","")
                                .replace("=",",")
                                .strip();
            String[] info = infoString.split(",");
            if (info.length<=1) continue;
            // 检查是否空的股票名
            if (null!=stockEmptyNameCodeList
                    && stockEmptyNameCodeList.size()>0
                    && stockEmptyNameCodeList.contains(info[0])){
                stockInfoCondition = new StockInfo();
                stockInfoCondition.setCode(info[0]);
                stockInfo = stockInfoService.getList(stockInfoCondition,1,1).getStockInfoList().get(0);
                stockInfo.setName(info[1]);
                stockInfoService.modify(stockInfo);
                stockEmptyNameCodeList.remove(info[0]);
            }
            // 将数组的内容放到实体中
            marketMinite = new MarketMinite();
            marketMinite.setCode(info[0]);
            marketMinite.setName(info[1]);
            marketMinite.setOpenPrice(info[2]);
            marketMinite.setYesterdayClosePrice(info[3]);
            marketMinite.setCurrentPrice(info[4]);
            marketMinite.setHighPrice(info[5]);
            marketMinite.setLowPrice(info[6]);
            marketMinite.setBidPrice(info[7]);
            marketMinite.setAskPrice(info[8]);
            marketMinite.setTradeVolume(Long.valueOf(info[9]));
            marketMinite.setTransactionAmount(info[10]);
            marketMinite.setBidVolume1(Integer.valueOf(info[11]));
            marketMinite.setBidQuote1(info[12]);
            marketMinite.setBidVolume2(Integer.valueOf(info[13]));
            marketMinite.setBidQuote2(info[14]);
            marketMinite.setBidVolume3(Integer.valueOf(info[15]));
            marketMinite.setBidQuote3(info[16]);
            marketMinite.setBidVolume4(Integer.valueOf(info[17]));
            marketMinite.setBidQuote4(info[18]);
            marketMinite.setBidVolume5(Integer.valueOf(info[19]));
            marketMinite.setBidQuote5(info[20]);
            marketMinite.setAskVolume1(Integer.valueOf(info[21]));
            marketMinite.setAskQuote1(info[22]);
            marketMinite.setAskVolume2(Integer.valueOf(info[23]));
            marketMinite.setAskQuote2(info[24]);
            marketMinite.setAskVolume3(Integer.valueOf(info[25]));
            marketMinite.setAskQuote3(info[26]);
            marketMinite.setAskVolume4(Integer.valueOf(info[27]));
            marketMinite.setAskQuote4(info[28]);
            marketMinite.setAskVolume5(Integer.valueOf(info[29]));
            marketMinite.setAskQuote5(info[30]);
            try {
                marketMinite.setMarketDay(dateFormat.parse(info[31]));
                marketMinite.setMarketTime(sdf.parse(info[31]+" "+info[32]));
            } catch (ParseException pe) {
                pe.printStackTrace();
            }
            marketMinite.setSource("hq.sinajs.cn");
            marketMinite.setTouchTime(new Date());
            marketMiniteService.add(marketMinite);
        }
        return true;
    }


    public boolean holdMarketDaily(String result){
        if (null==result || "".equals(result)) {
            System.out.println("holdMarketDaily -> result input errer -> " + result);
            return false;
        }
        String[] list = result.strip().split("var");
        MarketDaily marketDaily ;
        for (String s : list) {
            // 整理字符串，并分割出一个数组
            String infoString = s.strip()
                    .replace("hq_str_","")
                    .replace("\"","")
                    .replace(";","")
                    .replace("=",",")
                    .strip();
            String[] info = infoString.split(",");
            if (info.length<=1) continue;
            // 将数组的内容放到实体中
            marketDaily = new MarketDaily();
            marketDaily.setCode(info[0]);
            marketDaily.setName(info[1]);
            marketDaily.setOpenPrice(info[2]);
            marketDaily.setYesterdayClosePrice(info[3]);
            marketDaily.setCurrentPrice(info[4]);
            marketDaily.setHighPrice(info[5]);
            marketDaily.setLowPrice(info[6]);
            marketDaily.setBidPrice(info[7]);
            marketDaily.setAskPrice(info[8]);
            marketDaily.setTradeVolume(Long.valueOf(info[9]));
            marketDaily.setTransactionAmount(info[10]);
            marketDaily.setBidVolume1(Integer.valueOf(info[11]));
            marketDaily.setBidQuote1(info[12]);
            marketDaily.setBidVolume2(Integer.valueOf(info[13]));
            marketDaily.setBidQuote2(info[14]);
            marketDaily.setBidVolume3(Integer.valueOf(info[15]));
            marketDaily.setBidQuote3(info[16]);
            marketDaily.setBidVolume4(Integer.valueOf(info[17]));
            marketDaily.setBidQuote4(info[18]);
            marketDaily.setBidVolume5(Integer.valueOf(info[19]));
            marketDaily.setBidQuote5(info[20]);
            marketDaily.setAskVolume1(Integer.valueOf(info[21]));
            marketDaily.setAskQuote1(info[22]);
            marketDaily.setAskVolume2(Integer.valueOf(info[23]));
            marketDaily.setAskQuote2(info[24]);
            marketDaily.setAskVolume3(Integer.valueOf(info[25]));
            marketDaily.setAskQuote3(info[26]);
            marketDaily.setAskVolume4(Integer.valueOf(info[27]));
            marketDaily.setAskQuote4(info[28]);
            marketDaily.setAskVolume5(Integer.valueOf(info[29]));
            marketDaily.setAskQuote5(info[30]);
            try {
                marketDaily.setMarketDay(dateFormat.parse(info[31]));
                marketDaily.setMarketTime(sdf.parse(info[31]+" "+info[32]));
            } catch (ParseException pe) {
                pe.printStackTrace();
            }
            marketDaily.setSource("hq.sinajs.cn");
            marketDaily.setTouchTime(new Date());
            marketDailyService.add(marketDaily);
        }
        return true;
    }
}
